package com.corpgovernment.organization.dataloader.db;

import com.corpgovernment.api.organization.model.base.PageInfoResponse;
import com.corpgovernment.api.organization.model.employee.ListEmployeeByConditionRequest;
import com.corpgovernment.api.organization.model.employee.ListEmployeeByConditionResponse;
import com.corpgovernment.organization.entity.db.MbOrgEmployeeInfo;
import com.corpgovernment.organization.mapper.MbOrgEmployeeMapper;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import com.ctrip.corp.obt.generic.utils.StringUtils;
import com.ctrip.corp.obt.shard.context.PageContext;
import com.ctrip.corp.obt.shard.sql.mybatis.interceptor.pagination.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author jhxue
 * @create 2022-02-18-16:24
 */
@Component
public class MbOrgEmployeeLoader {

	@Autowired
	private MbOrgEmployeeMapper orgEmployeeMapper;

	public MbOrgEmployeeInfo selectOneByRecord(MbOrgEmployeeInfo record) {
		List<MbOrgEmployeeInfo> employeeInfoList = this.selectByRecord(record);
		if (CollectionUtils.isEmpty(employeeInfoList)) {
			return null;
		}
		return employeeInfoList.get(0);
	}

	public List<MbOrgEmployeeInfo> selectByRecord(MbOrgEmployeeInfo record) {
		if (record == null) {
			return new ArrayList<>();
		}
		record.setDeleteTime(new Date(0));
		return orgEmployeeMapper.select(record);
	}

	public List<MbOrgEmployeeInfo> selectByUids(List<String> uids) {
		if (CollectionUtils.isEmpty(uids)) {
			return new ArrayList<>();
		}
		Example example = new Example(MbOrgEmployeeInfo.class);
		example.createCriteria()
			   .andIn("uid", uids)
			   .andEqualTo("deleteTime", new Date(0));
		return orgEmployeeMapper.selectByExample(example);
	}

	public List<MbOrgEmployeeInfo> selectActiveEmployeeByUids(List<String> uids) {
		if (CollectionUtils.isEmpty(uids)) {
			return new ArrayList<>();
		}
		Example example = new Example(MbOrgEmployeeInfo.class);
		example.createCriteria()
				.andIn("uid", uids)
				.andEqualTo("deleteTime", new Date(0))
				.andEqualTo("valid", "T");
		return orgEmployeeMapper.selectByExample(example);
	}

	public MbOrgEmployeeInfo selectByUid(String uid) {
		Example example = new Example(MbOrgEmployeeInfo.class);
		example.createCriteria()
				.andEqualTo("uid", uid)
				.andEqualTo("deleteTime", new Date(0));
		List<MbOrgEmployeeInfo> mbOrgEmployeeInfoList = orgEmployeeMapper.selectByExample(example);
		if (CollectionUtils.isNotEmpty(mbOrgEmployeeInfoList)){
			return mbOrgEmployeeInfoList.get(0);
		}
		return null;
	}

	public boolean updateByUid(MbOrgEmployeeInfo record) {
		if (record == null || StringUtils.isBlank(record.getUid())) {
			return false;
		}
		Example example = new Example(MbOrgEmployeeInfo.class);
		example.createCriteria()
				.andEqualTo("uid", record.getUid()).andEqualTo("deleteTime", new Date(0));
		return orgEmployeeMapper.updateByExampleSelective(record, example) > 0;
	}

	public boolean create(MbOrgEmployeeInfo record) {
		return orgEmployeeMapper.insertSelective(record) > 0;
	}

	public List<MbOrgEmployeeInfo> selectByMobilePhoneList(List<String> mobilePhones) {
		if (CollectionUtils.isEmpty(mobilePhones)) {
			return new ArrayList<>();
		}
		Example example = new Example(MbOrgEmployeeInfo.class);
		example.createCriteria()
				.andIn("mobilePhone", mobilePhones)
				.andEqualTo("deleteTime", new Date(0));
		return orgEmployeeMapper.selectByExample(example);
	}

	public boolean updateALLByUid(MbOrgEmployeeInfo record) {
		if (record == null || StringUtils.isBlank(record.getUid())) {
			return false;
		}
		Example example = new Example(MbOrgEmployeeInfo.class);
		example.createCriteria()
				.andEqualTo("uid", record.getUid()).andEqualTo("deleteTime", new Date(0));
		return orgEmployeeMapper.updateByExampleSelective(record, example) > 0;
	}

	public boolean findRepeatName(String fullName){
		MbOrgEmployeeInfo record = new MbOrgEmployeeInfo();
		record.setDeleteTime(new Date(0));
		record.setName(fullName);
		List<MbOrgEmployeeInfo> orgEmployeeInfos = orgEmployeeMapper.select(record);
		return orgEmployeeInfos.size() > 0;
	}

    public List<MbOrgEmployeeInfo> listByCondition(ListEmployeeByConditionRequest request, List<String> uidList){
        List<MbOrgEmployeeInfo> employeeInfoList = orgEmployeeMapper.listEmployeeByCondition(request.getUid(), request.getEmployeeType(), uidList);
        if (StringUtils.isNotBlank(request.getName())){
            employeeInfoList = employeeInfoList.stream().filter(e -> e.getName().contains(request.getName())).collect(Collectors.toList());
        }
        if (StringUtils.isNotBlank(request.getPhoneNum())){
            employeeInfoList = employeeInfoList.stream().filter(e -> StringUtils.isNotBlank(e.getMobilePhone())).filter(e -> e.getMobilePhone().contains(request.getPhoneNum())).collect(Collectors.toList());
        }
        return employeeInfoList;
    }
}
